package com.enhinck.dbutilweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.enhinck.dbutilweb.common.WebResponse;
import com.enhinck.dbutilweb.common.database.ConnectionWrapper;
import com.enhinck.dbutilweb.common.database.DbPoolUtil;
import com.enhinck.dbutilweb.common.database.MysqlDBUtil;
import com.enhinck.dbutilweb.common.entity.ExcelFile;
import com.enhinck.dbutilweb.common.entity.InformationSchemaColumns;
import com.enhinck.dbutilweb.common.entity.InformationSchemaTables;
import com.enhinck.dbutilweb.common.entity.OneVersionModifySummary;
import com.enhinck.dbutilweb.common.main.MysqlDbCompare;
import com.enhinck.dbutilweb.common.main.MysqlDbToDictionary;
import com.enhinck.dbutilweb.common.word.CommonWordWriteUtil;
import com.enhinck.dbutilweb.config.SqlExecUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据源比较管理
 *
 * @author enhinck Generate auto created
 * @date 2021-03-03 10:52:04
 */
@Slf4j
@Api(description = "数据源查询工具", tags = {"DatasourceCompareController"})
@RestController
@AllArgsConstructor
public class DatasourceQueryController {
    private final DbPoolUtil dbPoolUtil;
    private final MysqlDBUtil mysqlDBUtil;

    /**
     * 数据库表列表
     *
     * @return
     */
    /**
     * @param id  库id
     * @param sql 表名
     * @return
     */
    @ApiOperation("数据库表中字段列表")
    @PostMapping("/datasource/query/sql")
    public WebResponse<List<Map<String, Object>>> datas(@RequestParam("id") Long id, @RequestParam("sql") String sql) {
        ConnectionWrapper connectionWrapper = null;
        try {
            connectionWrapper = dbPoolUtil.getConnection(id);
            List<Map<String, Object>> datas = mysqlDBUtil.selectSql(sql, connectionWrapper);
            connectionWrapper.setLastQueryTime(System.currentTimeMillis());
            return WebResponse.ok(datas);
        } catch (Exception e) {
            log.error("{}", e);
            return WebResponse.error("400", "数据库获取链接失败");
        } finally {
            dbPoolUtil.release(connectionWrapper);
        }
    }

    /**
     * 数据库表列表
     *
     * @return
     */
    /**
     * @param id  库id
     * @param ddl ddl语句
     * @return
     */
    @ApiOperation("数据库表中字段列表")
    @PostMapping("/datasource/query/ddl")
    public WebResponse<List<Map<String, Object>>> ddl(@RequestParam("id") Long id, @RequestParam("ddl") String ddl) {
        ConnectionWrapper connectionWrapper = null;
        try {
            connectionWrapper = dbPoolUtil.getConnection(id);
            SqlExecUtil.Result result = SqlExecUtil.execute(connectionWrapper.getConnection(), ddl);
            connectionWrapper.setLastQueryTime(System.currentTimeMillis());
            List<Map<String, Object>> datas = new ArrayList<>();
            Map<String, Object> data = new HashMap<>();
            data.putAll((JSONObject) JSONObject.toJSON(result));
            datas.add(data);
            return WebResponse.ok(datas);
        } catch (Exception e) {
            log.error("{}", e);
            return WebResponse.error("400", "数据库获取链接失败");
        } finally {
            dbPoolUtil.release(connectionWrapper);
        }
    }

}